import { getWebAuthenticationUrl, getWebOAuthToken, refreshOAuthToken, CozeAPI } from '@coze/api';
import redis from './redisClient.js';  // 注意：引入redis时也需要添加.js扩展名
import axios from 'axios';


const config = {
    "client_type": "web",
    "client_id": "26516874158112824643486830172721.app.coze",
    "coze_www_base": "https://www.coze.cn",
    "coze_api_base": "https://api.coze.cn",
    "client_secret": "wv1ADgNEe5u5pukUOwsvlvCt7lgBCrvJ4997gxDkadxDeXpY",
    "redirect_uris": [
        "http://localhost:3000/coze-auth-callback",
        "https://video-maker-api.sdjtwlgs.cn/coze-auth-callback",
        "http://127.0.0.1:8080/callback"
    ]
}
var REDIRECT_URI = ""
if (process.env.NODE_ENV === 'online'){
    REDIRECT_URI = "https://video-maker-api.sdjtwlgs.cn/coze-auth-callback";
}else{
    REDIRECT_URI = "http://localhost:3000/coze-auth-callback";
}


function timestampToDatetime(timestamp) {
    return new Date(timestamp * 1000).toLocaleString();
}


const AuthUrl = `https://www.coze.cn/api/permission/oauth2/authorize?response_type=code&client_id=26516874158112824643486830172721.app.coze&redirect_uri=http://localhost:3000/coze-auth-callback&state=1294848`

const coze = {
    getApiClient: async function () {
        var access_token = await redis.get("coze_api_access_token_web")
        if(!access_token){
            const refresh_token = await redis.get("coze_api_refresh_token_web")
            access_token = await this.refresh_token(refresh_token)
            
        }
        console.log(access_token)
        const apiClient = new CozeAPI({
            token: access_token,
            baseURL: 'https://api.coze.cn'
        });
        return apiClient
    },
    callback: async function (req, res) {

        const { code } = req.query;

        if (!code) {
            return res.send({
                "code": -1,
                "msg": "code为空，授权失败"
            })
        }

        try {
            // Get access token
            const oauthToken = await getWebOAuthToken({
                baseURL: config.coze_api_base,
                clientId: config.client_id,
                clientSecret: config.client_secret,
                code: code,
                redirectUrl: REDIRECT_URI,
            });

            console.log(oauthToken.expires_in)
            console.log("当前时间戳:", Math.floor(Date.now() / 1000));
            console.log("距离过期", oauthToken.expires_in - Math.floor(Date.now() / 1000));
            const ex_time = oauthToken.expires_in - Math.floor(Date.now() / 1000)
            // Render callback page
            const expiresStr = timestampToDatetime(oauthToken.expires_in);

            await redis.set("coze_api_access_token_web", oauthToken.access_token, "EX", ex_time)
            await redis.set("coze_api_refresh_token_web", oauthToken.refresh_token, "EX", 3600 * 24 * 30)

            return res.send({
                token_type: "web",
                access_token: oauthToken.access_token,
                refresh_token: oauthToken.refresh_token,
                expires_in: `${oauthToken.expires_in} (${expiresStr})`,
            })
        } catch (error) {
            console.error("Failed to get access token:", error);
            return res.send({
                "code": -1,
                "msg": error.message
            })
        }
    },
    refresh_token: async function () {
        try{
            const refresh_token = await redis.get("coze_api_refresh_token_web")
            const oauthToken = await refreshOAuthToken({
                baseURL: config.coze_api_base,
                clientId: config.client_id,
                clientSecret: config.client_secret,
                refreshToken: refresh_token,
            });

            const expiresStr = timestampToDatetime(oauthToken.expires_in);
            console.log({
                token_type: "web",
                access_token: oauthToken.access_token,
                refresh_token: oauthToken.refresh_token,
                expires_in: `${oauthToken.expires_in} (${expiresStr})`,
            })
            const ex_time = oauthToken.expires_in - Math.floor(Date.now() / 1000)
            await redis.set("coze_api_access_token_web", oauthToken.access_token, "EX", ex_time)
            await redis.set("coze_api_refresh_token_web", oauthToken.refresh_token, "EX", 3600 * 24 * 30)
            return oauthToken.access_token
        }catch(error){
            console.error("Failed to refresh token:", error);
            return ""
        }
        
    },
    //湘妹子配音
    voice1: async function (workflow_id) {
        try {
            var access_token = await redis.get("coze_api_access_token_web")
            if (!access_token) {
                access_token = await this.refresh_token()
            }
            console.log("access_token_web", access_token)
            const response = await axios({
                method: 'post',
                url: 'https://api.coze.cn/v1/workflow/stream_run',
                headers: {
                    'Authorization': `Bearer ${access_token}`,
                    'Content-Type': 'application/json'
                },
                data: {
                    workflow_id: workflow_id,
                    // parameters: {
                    //     url: url,
                    //     language: "汉语"
                    // }
                }
            });

            console.log(response.headers)
            console.log("插件原始字幕结果：", response.data);

            const messageDataMatch = response.data.match(/event:\s*message\s*data:\s*(\{[\s\S]*?)(?=\n(?:id:|event:|$))/i);

            if (messageDataMatch) {
                const messageData = messageDataMatch[1].trim();
                return JSON.parse(messageData);
            } else {
                throw new Error("未找到有效的 message data 内容");
            }

        } catch (error) {
            console.error("Failed to generate video caption:", error);
            throw error;
        }
}
}

export default coze